package com.example.afinal.Audio;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.afinal.R;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by HXL on 16/3/14.
 */
public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.ViewHolder>
        implements View.OnClickListener, View.OnLongClickListener {
    private Context mContext;
    List<File> list;
    private FileListAdapter.OnRecyclerViewItemClickListener mOnItemClickListener;
    private FileListAdapter.OnRecyclerViewItemLongClickListener mOnItemLongClickListener;

    public FileListAdapter() {
        list = new ArrayList<>();
    }

    public void setmNotes(List<File> files) {
        this.list = files;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (File) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemLongClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemLongClickListener.onItemLongClick(v, (File) v.getTag());
        }
        return true;
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, File file);
    }

    public void setOnItemClickListener(FileListAdapter.OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(View view, File file);
    }

    public void setOnItemLongClickListener(FileListAdapter.OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public FileListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Log.i(TAG, "###onCreateViewHolder: ");
        //inflate(R.layout.list_item_record,parent,false) 如果不这么写，cardview不能适应宽度
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_file_list, parent, false);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new FileListAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(FileListAdapter.ViewHolder holder, int position) {
        String time = null;
        //Log.i(TAG, "###onBindViewHolder: ");
        final File file = list.get(position);
        //将数据保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(file);
        //Log.e("adapter", "###record="+record);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
        try {
            Date date = df.parse(file.getName());
            time = date.toLocaleString();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        holder.adapterFileListName.setText(file.getName());
        holder.adapterFileListCreateTime.setText(time);
    }

    @Override
    public int getItemCount() {
        //Log.i(TAG, "###getItemCount: ");
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView adapterFileListName;   //语音文件时间
        public TextView adapterFileListCreateTime; //语音大小


        public ViewHolder(View view) {
            super(view);
            adapterFileListName = (TextView) view.findViewById(R.id.tv_voice_title);
            adapterFileListCreateTime = (TextView) view.findViewById(R.id.tv_voice_time);

        }
    }
}


